import Vue from "vue";
import Vuex from "vuex";

import allRoutes from '../router/allRoutes'
import recursionRoutes from "../utils/recursionRoutes"
import * as permissionApi from "../api/permission";
import * as studentApi from "../api/student";

import dynamicRoutes from "../router/dynamicRoutes";
import router from "../router"
Vue.use(Vuex);

export default new Vuex.Store({
  state: {

    userInfo: {},//用户信息
    sideMenu: [],//侧边栏菜单
    breadCrumb: [],//面包屑数据
    dataList: [],//获取到的学生信息
    selectList: [],//渲染的学生信息数组
    tableData: [],//当前表格渲染数据
    classList: [],//班级搜索的数组
    nameList: [],//姓名搜索的数组
    dataLength: 1,//表格渲染的个数
    pageSize: 5,//一页表格渲染的数量
    dialogTitle: "增加学员信息",
    currentRow: {},//编辑的时候数据
    updataSID:"",//需要修改的学员ID

    dialogFormVisible: false,//

  },
  mutations: {
    //设置用户信息
    SET_USERINFO(state, payload) {
      state.userInfo = payload
    },
    SET_SIDEMENU(state, payload) {
      let target = dynamicRoutes.find(route => route.path === "/")
      target.children = payload

      dynamicRoutes.forEach(route => {
        router.addRoute(route)
      })
      state.sideMenu = payload
    },

    SET_BREADCRUMB(state, payload) {//设置面包屑
      state.breadCrumb = payload
    },

    SET_DATALIST(state, payload) {//获取所有学生信息
      state.dataList = payload
    },
    SET_SELECTLIST(state, payload) {//获取需要渲染的学生信息数组
      state.selectList = payload
    },
    SET_TABLEDATA(state, payload) {//获取当前表格需要渲染的学生信息
      state.tableData = payload
    },
    SET_CLASSLIST(state, payload) {//获取班级搜索的学生信息
      state.classList = payload
    },
    SET_NAMELIST(state, payload) {//获取姓名搜索的学生信息
      state.nameList = payload
    },
    SET_DATALENGTH(state, payload) {//获取渲染数据个数
      state.dataLength = payload
    },
    SET_CURRENTROW(state, payload) {//获取编辑数据
      state.currentRow = { ...payload }
    },
    SET_UPDATASID(state, payload) {
      state.updataSID=payload
    }
    
  },
  actions: {
    async FETCH_MENULIST({ commit }) {
      let res = await permissionApi.getMenuListApi()
      let userRoutes = recursionRoutes(allRoutes, res.data.menuList)
      commit("SET_SIDEMENU", userRoutes)
    },

    //页面初始渲染数据
    async GET_DATA({ state, commit, dispatch }) {
      let res = await studentApi.getStudentList()
      console.log(res.data.data);
      let data = res.data.data;
      commit('SET_SELECTLIST', data);
      commit('SET_DATALIST', data)
      commit('SET_DATALENGTH', data.length)
      let NAMELIST = [], CLASSLIST = []
      state.dataList.forEach((item) => {
        NAMELIST.push({ value: item.name });
        CLASSLIST.push(item.class);
      });
      CLASSLIST = Array.from(new Set(CLASSLIST)).map((item, index) => {
        return { value: item, lable: index + 1 };
      });
      CLASSLIST.unshift({ value: "全部", lable: 0 });
      commit('SET_CLASSLIST', CLASSLIST);
      commit('SET_NAMELIST', NAMELIST);
      dispatch("GET_DATALIST", 1)
        ;
    },
    //翻页时改变当前页面需要渲染的列表tablaList
    GET_DATALIST({ state, commit }, page) {
      commit("SET_TABLEDATA", [])
      let TABLEDATA = []
      for (
        var i = (page - 1) * state.pageSize;
        i < state.dataLength && i < page * state.pageSize;
        i++
      ) {
        TABLEDATA.push({
          head: state.selectList[i].headimgurl,
          name: state.selectList[i].name,
          class: state.selectList[i].class,
          productUrl: state.selectList[i].productUrl,
          age: state.selectList[i].age,
          sId: state.selectList[i].sId,
          cTime: state.selectList[i].cTime,
          city: state.selectList[i].city,
          degree: state.selectList[i].degree,
          description: state.selectList[i].description,
          _id: state.selectList[i]._id,
        });
      }
      commit("SET_TABLEDATA", TABLEDATA)
      console.log(TABLEDATA)
      console.log(state.tableData)
    },
    CHANGECLASS({ state, commit, dispatch }, e) {//班级搜索选中
      let SELECTLIST = []
      if (e !== "全部") {
        SELECTLIST = state.dataList.filter((item) => {
          return item.class === e;
        });
      } else {
        SELECTLIST = state.dataList;
      }
      commit("SET_SELECTLIST", SELECTLIST)
      commit("SET_DATALENGTH", SELECTLIST.length)

      dispatch("GET_DATALIST", 1)
    },
    NAMESELECT({ state, commit, dispatch }, e) {//姓名搜索选中
      let SELECTLIST = []
      SELECTLIST = state.dataList.filter((item) => {
        return item.name === e.value;
      });
      commit("SET_SELECTLIST", SELECTLIST)
      commit("SET_DATALENGTH", SELECTLIST.length)
      dispatch("GET_DATALIST", 1)
    },
    NAMESEARCH({ state, commit, dispatch }, text) {//输入框回车或点击搜索
      let SELECTLIST = []
      SELECTLIST = state.dataList.filter((item) => {
        return item.name.includes(text);
      });
      commit("SET_SELECTLIST", SELECTLIST)
      commit("SET_DATALENGTH", SELECTLIST.length)
      console.log(state.selectList)
      dispatch("GET_DATALIST", 1)
    }


  },
  modules: {},
});
